<?php

namespace app\common\model;

use think\Db;

class ShopUserMoneyRecord extends BaseModel
{
    /**
     * 用户操作金额账单记录
     * @param $money 金额
     * @param $uid 用户id
     * @param $title 账单标题
     * @param int $type 1用户支出，2用户收入
     * @param string $desc
     * @return bool
     */
    public static function recordMoney($money,$uid,$title,$type=1,$desc=''){
        return self::record($money,$uid,$title,$type,true,$desc);
    }
    /**
     * 用户账单记录
     * @param $money 金额
     * @param $uid 用户id
     * @param $title 账单标题
     * @param int $type 1用户支出，2用户收入
     * @param bool $wallet 是否操作钱包增减
     * @param string $desc
     * @return bool
     */
    public static function record($money,$uid,$title,$type,$wallet=false,$desc=''){
        Db::startTrans();
        try {
            //记录账单
            self::create([
                'uid' => $uid,
                'title' => $title,
                'desc' => $desc,
                'money' => $money,
                'type' => $type,
            ]);
            if($wallet){
                //用户余额增减操作
                if($type == 1){
                    User::where('id',$uid)->setInc('money',$money);
                }elseif($type == 0){
                    User::where('id',$uid)->setDec('money',$money);
                }
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }
}
